/*
 * @Descripttion: 页面机构、单位筛选
 * @msg1: 系统管理员（TENANT_ADMIN）、机构管理员（ORG_ADMIN）：需要机构和单位筛选
 * @msg2: 联网单位管理员（UNIT_ADMIN）：只有一个单位，不用筛选
 * @msg3: 装维人员（INSTERALL_USER）：有一个或多个单位，需要单位筛选
 * @version: 
 * @Author: yuanliutech
 * @Date: 2025-03-12 16:56:41
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-18 15:00:36
 */
const app = getApp()
import {
  showMsg
} from '../../../utils/config.js'
import {
  reqOrgList
} from '../../../request/api'
module.exports = Behavior({
  data: {
    rolename: app.globalData.rolename,
    orgInfo: {}, // 机构/单位全部信息
    showUnitSelection: false,
  },
  lifetimes: {
    created() {
      console.log('create');
      app.globalData.orgInfo = null
      wx.setStorageSync('orgInfo', '');
      app.globalData.orgId = ''
      wx.setStorageSync('orgId', '');
      app.globalData.orgName = ''
      wx.setStorageSync('orgName', '');
    },
    attached() {
      console.log('attached');
      this.setData({
        rolename: app.globalData.rolename
      })
    },
    detached() {
      console.log('detached');
      app.globalData.orgInfo = null
      wx.setStorageSync('orgInfo', '');
      app.globalData.orgId = ''
      wx.setStorageSync('orgId', '');
      app.globalData.orgName = ''
      wx.setStorageSync('orgName', '');
    }
  },
  pageLifetimes: {
    show() {
      this.setData({
        rolename: app.globalData.rolename
      })
    },

  },
  methods: {

    // 机构选择页面
    onOrgSelection() {
      wx.navigateTo({
        url: `/pages/personal-inside-page/org/org?mark=personalCenter`,
        events: {
          acceptData: (res) => {
            if (res.orgId === this.data.orgInfo.orgId) return
            this.setData({
              orgInfo: res
            })

            this.confirmOrg()
          },
        },
      })
    },


    // 单位选择弹窗打开
    onUnitSelection() {
      this.setData({
        showUnitSelection: true
      })
    },
    confirmUnitSelection(e) {
      const {
        msg,
        showUnitSelection,
        orgInfo
      } = e.detail

      this.setData({
        showUnitSelection,
      })

      if (msg) {
        if (this.data.orgInfo.orgId === orgInfo.id) return
        this.setData({
          orgInfo: {
            orgId: orgInfo.id,
            id: orgInfo.id,
            orgName: orgInfo.orgName,
            type: orgInfo.type
          }
        })
        this.confirmUnit()
      }
    },
  }
})